public class Solution134 {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int last=-1,cur=-1;
        for (int i=0;i<gas.length*2;i++){
            int g=gas[i % gas.length]-cost[i % gas.length];
            g=last>0?g+last:g;
            last=g;
            if (g>=0 && i-cur==gas.length){
                return cur+1;
            }
            if (g<0){
                cur=i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new Solution134().canCompleteCircuit(new int[]{1,2,3,4,5},new int[]{3,4,5,1,2}));
    }
}
